-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable checking for f strings #17732
base: master
Are you sure you want to change the base?
Enable checking for f strings #17732
Conversation
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: websockets (https://github.com/aaugustin/websockets)
+ src/websockets/sync/messages.py:102: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float") [str-format]
pandas (https://github.com/pandas-dev/pandas)
+ pandas/tests/scalar/test_na_scalar.py:38: error: Unrecognized format specification "xxx" [str-format]
pwndbg (https://github.com/pwndbg/pwndbg)
+ pwndbg/gdblib/symbol.py: note: In function "get":
+ pwndbg/gdblib/symbol.py:100: error: Incompatible types in string interpolation (expression has type "Value", placeholder has type "int") [str-format]
freqtrade (https://github.com/freqtrade/freqtrade)
+ freqtrade/persistence/trade_model.py:852: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float") [str-format]
+ freqtrade/rpc/telegram.py:399: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float") [str-format]
+ freqtrade/rpc/telegram.py:421: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float") [str-format]
+ freqtrade/rpc/telegram.py:446: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float") [str-format]
+ freqtrade/strategy/interface.py:1511: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int | float") [str-format]
aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:1164:32: error: Incompatible types in string interpolation (expression has type "None", placeholder has type "int") [str-format]
+ aiohttp/connector.py:1164:32: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-str-format for more info
materialize (https://github.com/MaterializeInc/materialize)
+ misc/python/materialize/feature_benchmark/comparator.py:56: error: Incompatible types in string interpolation (expression has type "T", placeholder has type "int | float") [str-format]
+ misc/python/materialize/feature_benchmark/comparator.py:65: error: Incompatible types in string interpolation (expression has type "T", placeholder has type "int | float") [str-format]
ignite (https://github.com/pytorch/ignite)
+ ignite/utils.py:134: error: Incompatible types in string interpolation (expression has type "Number", placeholder has type "int | float") [str-format]
+ ignite/utils.py:165: error: Incompatible types in string interpolation (expression has type "Number", placeholder has type "int | float") [str-format]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to mypy-issues, this PR also fixes:
(I just realized after I ran it that that issue is mentioned in 17714 but whatever.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should go to test-data/unit/check-formatting.test
if spec.format_spec == ":{}": # most simple dynamic case | ||
assert ( | ||
expression is not None | ||
) # dynamic spec cannot be last, this should have been detected earlier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a test case? I imagine just "{:{}}".format("aaaa")
would work... (I'm not sure what could possibly trigger this though)
new_format_string = f"{{{spec.conversion or ''}:{next_expression.value}}}" | ||
parsed = parse_format_value(new_format_string, call, self.msg) | ||
if parsed is None or len(parsed) != 1: | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this happens if the resulting format string isn't a valid format string. Is that caught elsewhere, or does that deserve a self.msg.fail
here?
This implements #17714
Support for f-string checking was already partially implemented. Consider this code:
Mypy ran these lines through the format checker. The call expressions look like this:
The difference is, that the actually static
"abc"
is converted to a "dynamic" spec.This MR attempts to enable type checking of f-strings by inlining these "semi dynamic" specs.
Open Questions:
pytest mypy/test/testcheck.py::TypeCheckSuite::check-string-format.test
. It never finds any errors even when it should clearly failChecklist